home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / objintpt.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  2KB  |  73 lines

  1.                                  // Chapter 6 - Program 3
  2. #include <iostream.h>
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    int *point;
  8. public:
  9.    box(void);         //Constructor
  10.    void set(int new_length, int new_width, int stored_value);
  11.    int get_area(void) {return length * width;}  // Inline
  12.    int get_value(void) {return *point;}         // Inline
  13.    ~box();            //Destructor
  14. };
  15.  
  16.  
  17. box::box(void)        //Constructor implementation
  18. {
  19.    length = 8;
  20.    width = 8;
  21.    point = new int;
  22.    *point = 112;
  23. }
  24.  
  25.  
  26. // This method will set a box size to the input parameters
  27. void box::set(int new_length, int new_width, int stored_value)
  28. {
  29.    length = new_length;
  30.    width = new_width;
  31.    *point = stored_value;
  32. }
  33.  
  34.  
  35. box::~box(void)       //Destructor
  36. {
  37.    length = 0;
  38.    width = 0;
  39.    delete point;
  40. }
  41.  
  42.  
  43. main()
  44. {
  45. box small, medium, large;          //Three boxes to work with
  46.  
  47.    small.set(5, 7, 177);
  48.    large.set(15, 20, 999);
  49.    
  50.    cout << "The small box area is " << small.get_area() << "\n";
  51.    cout << "The medium box area is " << medium.get_area() << "\n";
  52.    cout << "The large box area is " << large.get_area() << "\n";
  53.    cout << "The small box stored value is " << 
  54.                                           small.get_value() << "\n";
  55.    cout << "The medium box stored value is " << 
  56.                                           medium.get_value() << "\n";
  57.    cout << "The large box stored value is " << 
  58.                                           large.get_value() << "\n";
  59. }
  60.  
  61.  
  62.  
  63.  
  64. // Result of execution
  65. //
  66. // The small box area is 35
  67. // The medium box area is 64
  68. // The large box area is 300
  69. // The small box stored value is 177
  70. // The medium box stored value is 112
  71. // The large box stored value is 999
  72.  
  73.